﻿
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Xcj.Mes.DataBase.Service;
using Xcj.Mes.WholeLine.DataBase.Entity;

namespace Xcj.Mes.WholeLine.ZH.HG.Shell.ViewModels.Acoount
{
    public class AccountViewModel : BaseViewModel
    {
        private string _adminPassword = string.Empty;
        public string AdminPassword
        {
            get => _adminPassword;
            set
            {
                _adminPassword = value;
                RaisePropertyChanged();
            }
        }

        private string _adminAffirmPassword = string.Empty;
        public string AdminAffirmPassword
        {
            get => _adminAffirmPassword;
            set
            {
                _adminAffirmPassword = value;
                RaisePropertyChanged();
            }
        }

        private string _operatorPassword = string.Empty;
        public string OperatorPassword
        {
            get => _operatorPassword;
            set
            {
                _operatorPassword = value;
                RaisePropertyChanged();
            }
        }

        private string _operatorAffirmPassword = string.Empty;
        public string OperatorAffirmPassword
        {
            get => _operatorAffirmPassword;
            set
            {
                _operatorAffirmPassword = value;
                RaisePropertyChanged();
            }
        }
        
        public bool IsButtonEnabled => (App.accountEntity.Role == "管理员");

        public AccountViewModel() { }

        public void OnClear(object source, object eventArgs)
        {
            AdminPassword          = string.Empty;
            AdminAffirmPassword    = string.Empty;
            OperatorPassword       = string.Empty;
            OperatorAffirmPassword = string.Empty;
        }

        public void OnSave(object source, object eventArgs)
        {
            if (string.IsNullOrEmpty(AdminPassword) && string.IsNullOrEmpty(OperatorPassword))
            {
                MessageBox.Show("管理员和操作员密码不能为空！");
                return;
            }

            if(!string.IsNullOrEmpty(AdminPassword))
            {
                if (string.IsNullOrEmpty(AdminAffirmPassword))
                {
                    MessageBox.Show("管理员再次输入密码不能为空！");
                    return;
                }

                if (AdminPassword != AdminAffirmPassword)
                {
                    MessageBox.Show("管理员密码与再次输入密码不符！");
                    return;
                }

                using (AccountService service = new AccountService())
                {
                    List<AccountEntity> list = service.All()?.OrderByDescending((o)=>o.CreateTime).Where((o)=>o.Role== "管理员")?.Take(1).ToList();
                
                    if((list?.Count??0)<=0)
                    {
                        AccountEntity account = new AccountEntity()
                        {
                            UserName = "管理员",
                            UserID   = "1",
                            Password = AdminPassword,
                            Role     = "管理员",
                            Enable   = true
                        };
                        service.Add(account);
                    }
                    else
                    {
                        list[0].Password = AdminPassword;
                        service.Edit(list[0]);
                    }
                }
            }

            if (!string.IsNullOrEmpty(OperatorPassword))
            {
                if (string.IsNullOrEmpty(OperatorAffirmPassword))
                {
                    MessageBox.Show("操作员再次输入密码不能为空！");
                    return;
                }

                if (OperatorPassword != OperatorAffirmPassword)
                {
                    MessageBox.Show("操作员密码与再次输入密码不符！");
                    return;
                }

                using (AccountService service = new AccountService())
                {
                    List<AccountEntity> list = service.All()?.OrderByDescending((o) => o.CreateTime).Where((o) => o.Role == "操作员")?.Take(1).ToList();

                    if ((list?.Count ?? 0) <= 0)
                    {
                        AccountEntity account = new AccountEntity()
                        {
                            UserName = "操作员",
                            UserID   = "2",
                            Password = OperatorPassword,
                            Role     = "操作员",
                            Enable   = true
                        };
                        service.Add(account);
                    }
                    else
                    {
                        list[0].Password = OperatorPassword;
                        service.Edit(list[0]);
                    }
                }
            }
        }
    }
}
